package zhihuilan.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;
import zhihuilan.dto.WxMiniPageDto;
import zhihuilan.entity.MyShopHonor;
import zhihuilan.mapper.MyShopHonorMapper;
import zhihuilan.params.query.MyShopHonorQueryParam;
import zhihuilan.service.MyShopHonorService;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class MyShopHonorServiceImpl extends ServiceImpl<MyShopHonorMapper, MyShopHonor> implements MyShopHonorService {


    @Override
    public WxMiniPageDto<MyShopHonor> selectPageList(MyShopHonorQueryParam queryParam){
        QueryWrapper<MyShopHonor> queryWrapper = new QueryWrapper<>();
        if (ObjectUtils.isEmpty(queryParam)) {
            return null;
        }
        if (!ObjectUtils.isEmpty(queryParam.getStatus())) {
            queryWrapper.eq("status", queryParam.getStatus());
        }else{
            queryWrapper.eq("status", 1);
        }
        if (!ObjectUtils.isEmpty(queryParam.getShopid())) {
            queryWrapper.eq("shopid", Integer.parseInt(queryParam.getShopid()));
        }
        if (!ObjectUtils.isEmpty(queryParam.getPid())) {
            queryWrapper.eq("pid", queryParam.getPid());
        }
        queryWrapper.orderByDesc("id");
        long pageNum = 0;
        long pageSize = 10;
        if (queryParam.getPage() >= 0L) {
            pageNum = queryParam.getPage();
        }
        if(pageNum==1){
            pageNum=0;
        }
        if (queryParam.getPageSize() > 0L) {
            pageSize = queryParam.getPageSize();
        }
        Page<MyShopHonor> page = new Page<>(pageNum, pageSize);
        this.baseMapper.selectPage(page, queryWrapper);
        WxMiniPageDto<MyShopHonor> result = new WxMiniPageDto(pageNum, pageSize);
        BeanUtils.copyProperties(page, result);
        result.setData(page.getRecords());
        return result;
    }

    @Override
    public List<MyShopHonor> selectList(MyShopHonor queryParam) {
        QueryWrapper<MyShopHonor> wrapper = new QueryWrapper<>();
        if (ObjectUtils.isEmpty(queryParam)) {
            return null;
        }
        if (!ObjectUtils.isEmpty(queryParam.getShopid())) {
            wrapper.eq("shopid", queryParam.getShopid());
        }
        wrapper.orderByAsc("id");
        return this.baseMapper.selectList(wrapper);
    }

    @Override
    public MyShopHonor selectById(Integer id) {
        if (ObjectUtils.isEmpty(id)) {
            return null;
        }
        return this.baseMapper.selectById(id);
    }

    @Transactional
    @Override
    public MyShopHonor addEntity(MyShopHonor entity) throws RuntimeException {
        if (ObjectUtils.isEmpty(entity)) {
            return null;
        }
        if (!ObjectUtils.isEmpty(entity.getId())) {
            return null;
        }
        this.baseMapper.insert(entity);
        return entity;
    }

    @Override
    public MyShopHonor updateEntity(MyShopHonor entity) {
        if (ObjectUtils.isEmpty(entity)) {
            return null;
        }
        if (ObjectUtils.isEmpty(entity.getId())) {
            return null;
        }
        this.baseMapper.updateById(entity);
        return entity;
    }

    /**
     * 根据主键删除购物车物品
     */
    @Override
    public Boolean deletetById(Integer id) {
        if (ObjectUtils.isEmpty(id)) {
            return false;
        }
        int result = this.baseMapper.deleteById(id);
        return result > 0;
    }
}
